package br.com.dev.tcc.view.utilitarios;

import java.util.List;

import android.app.Activity;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.animation.AlphaAnimation;
import android.view.animation.Animation;
import android.view.animation.DecelerateInterpolator;
import android.view.animation.RotateAnimation;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import br.com.dev.tcc.R;
import br.com.dev.tcc.bean.UtilitariosViewItem;
import br.com.dev.tcc.interfaces.UtilitariosItemView;

public class UtilitariosView {

	private Context ctx;
	private List<UtilitariosViewItem> itens;

	private LinearLayout layoutPai;
	//private RotateAnimation rotateOpenAnim;
	//private RotateAnimation rotateCloseAnim;
	private AlphaAnimation alphaAnim;
	private Listener listener;

	public UtilitariosView(Activity ctx) {

		this.ctx = ctx;
		listener = new Listener();
		layoutPai = (LinearLayout) ctx.findViewById(R.id.layoutToList);
		setAnimations();
	}

	private void setAnimations() {

		/*rotateOpenAnim = new RotateAnimation(0, 180,
				Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF,
				0.5f);
		rotateOpenAnim.setFillAfter(true);
		rotateOpenAnim.setDuration(300);

		rotateCloseAnim = new RotateAnimation(180, 360,
				Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF,
				0.5f);
		rotateOpenAnim.setFillAfter(true);
		rotateCloseAnim.setDuration(300);*/
		
		alphaAnim = new AlphaAnimation(0, 1);
		alphaAnim.setDuration(400);
		alphaAnim.setInterpolator(new DecelerateInterpolator());
		alphaAnim.setFillAfter(true);
	}

	public void setViews(List<UtilitariosViewItem> itens) {

		this.itens = itens;
		buildViewItens();
	}

	private void buildViewItens() {

		LayoutInflater inflater = LayoutInflater.from(ctx);

		for (UtilitariosViewItem item : itens) {

			View view = inflater.inflate(R.layout.utilitarios_item, null);

			TextView t = (TextView) view.findViewById(R.id.tvTexto);
			t.setText(item.getText());

			ImageView i = (ImageView) view.findViewById(R.id.ivIcone);
			i.setImageResource(item.getIcon());

			view.setOnClickListener(listener);

			setViewToLayout(view, item);

			layoutPai.addView(view);
		}
	}

	private void setViewToLayout(View view, UtilitariosViewItem item) {

		LinearLayout layout = (LinearLayout) view
				.findViewById(R.id.layoutToView);

		UtilitariosItemView uvi = item.getViewItemToInflate();
		if (uvi != null)
			layout.addView(uvi.getView());
	}

	class Listener implements OnClickListener {

		@Override
		public void onClick(View v) {

			View layout = v.findViewById(R.id.layoutToView);

			if (layout.getVisibility() == View.VISIBLE){
			
				//v.findViewById(R.id.ivSeta).startAnimation(rotateCloseAnim);
				layout.setVisibility(View.GONE);
			}
			else{
			
				//v.findViewById(R.id.ivSeta).startAnimation(rotateOpenAnim);
				layout.startAnimation(alphaAnim);
				layout.setVisibility(View.VISIBLE);
			}
		}
	}
}
